<script setup lang="ts">
import { provide, ref, inject ,onMounted} from "vue";
import TagSearch from "@renderer/view/tagPage/components/TagSearch.vue";
import TagList from "@renderer/view/tagPage/components/TagList.vue";
import API from "@renderer/api/api";
import { postRequest } from "@renderer/api/request";
import { getActiveTabViewOp } from "@renderer/view/hooks/common";
const searchRef = ref('')
const {
  activeViewRef: activeView
} = getActiveTabViewOp()
const currentDicomIndex = 0
const tagsInfo = ref([])
const {
  showSnackBar,
} = inject('snackBar') as any;
const loadTagInfo = async () => {
  const {data} = await postRequest(API.getTags, {
    seriesId: activeView.value.seriesId,
    index: 0
  })
  tagsInfo.value = data.tagsInfo
}

onMounted(() => {
  loadTagInfo()
})

// let testItems:any[] = []
// for (let i = 0; i < 10; i++){
//   testItems.push({
//     tagId: i,
//     description: i,
//     value: i,
//     dicomIndex: 0,
//     seriesId: ''
//   })
// }
//
// const tagsInfo = ref(testItems)


const isActive = ref(false)

const applySeries =async () => {
  const mapTagInfo = copySelected.value.map(tagInfo => {
    return {
      tagId: tagInfo.tagId,
      value: tagInfo.value,
    }
  })
  await postRequest(API.modifySeries, {
    tagInfo: mapTagInfo,
    seriesId: currentSeriesId,
    saveFilePath: saveFilePath.value
  })
}

const applyCurrentDicom = async () => {
  const mapTagInfo = copySelected.value.map(tagInfo => {
    return {
      tagId: tagInfo.tagId,
      value: tagInfo.value,
    }
  })
  await postRequest(API.modifyDicom, {
    tagInfo: mapTagInfo,
    seriesId: currentSeriesId,
    dicomIndex: currentDicomIndex,
    saveFilePath: saveFilePath.value
  })
  showSnackBar({
    text: '修改成功'
  })
}

const saveFilePath = ref('C:\\Users\\57693\\Desktop\\test')

const handleChooseFilePath = async() => {
  const newFolderPath = await (window as any).folderApi.selectFolder()
  if (newFolderPath){
    saveFilePath.value = newFolderPath
  }
}
const selected = ref<any[]>([]);

const copySelected = ref<any[]>([]);

provide('tagInfo', {
  searchVal: searchRef,
  tagsInfo,
  showEditTag: isActive,
  selected:selected,
  copySelected:copySelected,
})
</script>

<template>
  <div class="flex w-full h-full flex-col !p-[10px]">
          <v-card class="!mb-[10px] h-[50px] " >
            <tag-search />
          </v-card>

        <div class=" h-[calc(100%-150px)]">
          <tag-list/>
        </div>
  </div>


  <v-dialog max-width="700" v-model="isActive" scrollable>
    <v-card
      max-width="700"
      class="!p-[10px]"
    >
      <v-card-title>
        <div class="flex justify-space-between items-center">
          <div>
            <v-icon icon="mdi-tag-edit"></v-icon>
            编辑标签
          </div>

          <v-btn
            icon="mdi-close"
            variant="text"
            @click="isActive = false"
          ></v-btn>
        </div>

      </v-card-title>

      <v-card-text class="h-[300px]">
        <v-form >
          <v-container >
            <v-row v-for="item in copySelected" dense>

              <v-col>
                <v-text-field
                  density="compact"
                  v-model="item.tagId"
                  disabled
                  label="Tag ID"
                ></v-text-field>
              </v-col>

              <v-col>
                <v-text-field
                  density="compact"
                  v-model="item.description"
                  disabled
                  label="Description"
                ></v-text-field>
              </v-col>

              <v-col>
                <v-text-field
                  density="compact"
                  v-model="item.value"
                  label="Value"
                ></v-text-field>
              </v-col>
            </v-row>
          </v-container>
        </v-form>
      </v-card-text>


      <v-card-actions>
        <v-row class="!pl-[32px]">
          <v-col :cols="8" >
            <v-text-field
              v-model="saveFilePath"
              readonly
              @click:control="handleChooseFilePath"
              label="保存生成的Dicom路径"
            ></v-text-field>
          </v-col>
          <v-col :cols="4">
            <v-checkbox disabled :model-value="true" label="采用原文件名称"></v-checkbox>
          </v-col>
        </v-row>
      </v-card-actions>

      <v-card-actions>
        <v-btn :disabled="saveFilePath ===''" color="active" @click="applySeries">应用序列</v-btn>
        <v-btn :disabled="saveFilePath ===''" @click="applyCurrentDicom">应用该文件</v-btn>
        <v-btn @click="isActive = false">取消</v-btn>
      </v-card-actions>
    </v-card>

  </v-dialog>
</template>

<style scoped>

</style>
